import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/api/home.dart';
import 'package:flutter_enjoy_plus/pages/home/component/home_list.dart';
import 'package:flutter_enjoy_plus/pages/home/component/home_nav.dart';
import 'package:flutter_enjoy_plus/utils/TokenManager.dart';
import 'package:flutter_enjoy_plus/utils/toast.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  _IndexState createState() => _IndexState();
}

class _IndexState extends State<HomePage> {

  // 社区公告
  List AnnounceLIst = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getAnnounceList();
  }

  getAnnounceList() async {
    final res = await getAnnounceListAPI();
    PrompAction.showSuccess('获取社区公告成功');
    setState(() {
      AnnounceLIst = res;
    });
    // print(AnnounceLIst);
  }

  Timer?  _timer;
  int _count_down = 60;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromRGBO(242, 245, 255,1),
      appBar: AppBar(
        title: const Text("享+社区"),
        centerTitle: true,
        backgroundColor: Colors.transparent,
      ),
      body: ListView(
        padding: const EdgeInsets.all(10),
        children: [
          // 测试弹框
          // ElevatedButton(
          //   onPressed: (){
          //     // 日期选择器
          //     // showDatePicker(
          //     //   context: context,
          //     //   initialDate: DateTime.now(),
          //     //   firstDate: DateTime(2000),
          //     //   lastDate: DateTime(2025),
          //     // ).then((value) {
          //     //   print('选择的日期是:$value');
          //     // });
          //     // 
          //     showAboutDialog(context: context,children: [
          //       const Text('测试弹框内容'),
          //       const Text('测试弹框内容'),
          //       const Text('测试弹框内容'),
          //       const Text('测试弹框内容')
                
          //     ]);
          //   },
          //   child: const Text('测试弹框')),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Expanded(
                child: ElevatedButton(
                  onPressed: () {
                    tokenmanager.setToken('123456789');
                  },
                  child: const Text('存储token'),
                ),
              ),
              Expanded(
                child: ElevatedButton(
                  onPressed: () {
                    _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
                      _count_down--;
                      setState(() {});
                    });
                   },
                  child: const Text('开始定时器'),
                ),
              ),
              Text('倒计时：$_count_down'),
              Expanded(
                child: ElevatedButton(
                  onPressed: () {
                    _timer?.cancel();
                  },
                  child: const Text('停止定时器'),
                ),
              ),
              Expanded(
                child: ElevatedButton(
                  onPressed: () {},
                  child: const Text('延时器'),
                ),
              ),
            ],
          ),


          // 导航条
          const HomeNav(),
          // 图片区域
          ClipRRect(
            borderRadius: const BorderRadius.all(Radius.circular(10)),
            child: Image.asset('assets/images/banner@2x.jpg')
          ),

          // 社区公告
          HomeList(
            notifyList:AnnounceLIst,
          )
        ]
      ),
    );
  }
}
